#!/data/data/com.termux/files/usr/bin/bash
DIR=$(
  cd $(dirname $0)
  pwd
)
cur=$(pwd)
THEME_DIR=${DIR}/themes
count=0

echo -e "The default theme is agnoster.\nYou can choose another one from the list below"
echo '您可以在此列表中选择zsh主题。'
for themes in "$THEME_DIR"/*; do
  themes_name[count]=$(echo $themes | awk -F'/' '{print $NF}')
  echo -e "($count) ${themes_name[count]}"
  count=$(($count + 1))
done
count=$(($count - 1))

while true; do
  read -p '请输入选项数字,并按回车键,留空不更改。Please type the option number and press Enter:' number
  if [[ -z "$number" ]]; then
    break
  elif ! [[ $number =~ ^[0-9]+$ ]]; then
    echo "Please enter the right number!"
  elif (($number >= 0 && $number <= $count)); then
    eval choice=${themes_name[number]}

    if [ "${choice}" = "powerlevel9k" ]; then
      cp -rf "$THEME_DIR/${choice}" "${DIR}/../.oh-my-zsh/custom/themes/${choice}"
      sed -i '/^ZSH_THEME/d' "${HOME}/.zshrc"
      sed -i "1iZSH_THEME="${choice}/${choice}"" "${HOME}/.zshrc"
    elif [ "${choice}" = "powerlevel10k" ]; then
      sed -i '/^ZSH_THEME/d' "${HOME}/.zshrc"
      sed -i "1 i\ZSH_THEME='${choice}/${choice}'" "${HOME}/.zshrc"
      if [ -d "${DIR}/../.oh-my-zsh/custom/themes/${choice}/.git" ]; then
        cd "${DIR}/../.oh-my-zsh/custom/themes/${choice}"
        git reset --hard origin/master
        git pull --depth=1 origin master
        cd ${cur}
      else
        rm -rf "${DIR}/../.oh-my-zsh/custom/themes/${choice}"
        git clone https://gitee.com/mo2/powerlevel10k.git "${DIR}/../.oh-my-zsh/custom/themes/${choice}" --depth=1
        echo '检测到您选择的是powerlevel 10k主题,若无法弹出配置面板，则请拉宽屏幕显示大小，然后输p10k configure'
      fi

    else

      cp -rf "$THEME_DIR/${choice}" "${DIR}/../.oh-my-zsh/custom/themes/${choice}.zsh-theme"
      sed -i '/^ZSH_THEME/d' "${HOME}/.zshrc"
      sed -i "1iZSH_THEME="${choice}"" "${HOME}/.zshrc"
    fi
    break
  else
    echo -e "Please restart termux app!\n若修改完成后未生效，则建议您重启终端。"
  fi
done
source "${HOME}/.zshrc" 2>/dev/null
if [ "$(uname -o)" = "Android" ]; then
  chsh -s zsh
fi
echo -e "Please restart termux app!\n若修改完成后未生效，则建议您重启终端。"
zsh
